//
//  TextIndicatorView.m
//  彩宝宝
//
//  Created by xh on 16/1/12.
//  Copyright © 2016年 xh. All rights reserved.
//

#define KPadding 20

#import "TextIndicatorView.h"

@implementation TextIndicatorView

- (instancetype)initWithShowMessage:(NSString *)message{
    CGSize textSize=[self  getNSStingFormCGSize:message textFont:14];
    
    CGFloat lableWidth=textSize.width+40;
    
    self = [super initWithFrame:CGRectMake((LSScreenWidth-lableWidth)/2, LSScreenHeight*0.45, lableWidth, textSize.height+20)];
    if(self){
        [self setBackgroundColor:[UIColor blackColor]];
        self.layer.masksToBounds=YES;
        self.layer.cornerRadius=6;
        
        UIButton *titleButton=[[UIButton alloc]init];
        [titleButton setTitle:message forState:UIControlStateNormal];
       
        [titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [titleButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [titleButton setContentMode:UIViewContentModeCenter];
        [titleButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
        titleButton.titleLabel.numberOfLines=5;
        [titleButton setFrame:CGRectMake(10, 5, CGRectGetWidth(self.bounds)-20, CGRectGetHeight(self.bounds)-10)];
        [titleButton setBackgroundColor:[UIColor blackColor]];
        [self addSubview:titleButton];
        
    }
    return self;
}



-(void)showViewAfterDelay:(NSTimeInterval)delay{
    UIWindow *lastWindow ;//= [[UIApplication sharedApplication].windows lastObject];
        if (@available(iOS 11.0, *)) {
            lastWindow = [UIApplication sharedApplication].keyWindow;
        }else{
            lastWindow = [[UIApplication sharedApplication].windows lastObject];
        }
    
    [lastWindow addSubview:self];
    
    //3秒后执行删除视图
    [self performSelector:@selector(hide) withObject:self afterDelay:delay];

    
}



- (void)hide{
    [UIView animateWithDuration:1.0 animations:^{
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}



-(CGSize)getNSStingFormCGSize:(NSString *)titleStr textFont:(NSInteger)fontSize{
    UIFont *fontOne = [UIFont systemFontOfSize:fontSize];//设置字体大小
    CGSize maximumLabelSizeOne = CGSizeMake([UIScreen mainScreen].bounds.size.width*0.4,MAXFLOAT);
    CGSize textSize=[titleStr boundingRectWithSize:maximumLabelSizeOne options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:fontOne} context:nil].size;
    return textSize;
}


@end
